package org.xhy.ruleengine.sample;

import org.xhy.ruleengine.groovy.GroovyClassLoader;
import org.xhy.ruleengine.tree.Node;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-06-23 14:17
 */
public class Sample {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {


        final Node node = new Node();

        final Node  node1 = new Node();
        node1.setType(true);
        node1.setExpression("{user.name} == 'xhy'");

        final Node node2 = new Node();
        node2.setType(false);
        node2.setExpression("{user.name} == '小虎呀'");
        node2.setResult("hsd");

        final Node node3 = new Node();
        node3.setExpression("{user.age} > 5");
        node3.setType(false);
        node3.setResult("sadasdsad");

        node1.setChildNodes(Arrays.asList(node3));
        node.setChildNodes(Arrays.asList( node1,node2));

        final HashMap<String, Object> map = new HashMap<>();
        map.put("user.name","xhy");
        map.put("user.age",18);

        final GroovyClassLoader groovyClassLoader = new GroovyClassLoader();
        final Class clazz = groovyClassLoader.generateClass(node1.getChildNodes());

        final Object o = clazz.newInstance();
        final Object invoke = clazz.getMethod("exec", Map.class).invoke(o, map);
        System.out.println(invoke);
    }
    static class User implements Serializable {
        Integer age = 18;
        String name = "xhy";
        Address address = new Address();

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }
    }
    static class Address implements Serializable {
        String ip = "1";
        String host = "80";

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public String getHost() {
            return host;
        }

        public void setHost(String host) {
            this.host = host;
        }
    }
}
